//
//  WKWebViewController.swift
//  httpsTest
//
//  Created by laoJun on 2016/12/20.
//  Copyright © 2016年 Kuick. All rights reserved.
//

import UIKit
import WebKit
class WKWebViewController: UIViewController,WKNavigationDelegate {

    
    var webView :WKWebView?
    var request :URLRequest?
    var textField :UITextField?
    
    init(rquest:URLRequest) {
        super.init(nibName: nil, bundle: nil)
        request = rquest
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addSubView()
        let request = URLRequest.init(url: URL.init(string: "https://www.baidu.com")!)
         webView?.load(request)
        // Do any additional setup after loading the view.
        addTextView()
    }
    
    func loadRequset(_ rquest:URLRequest) {
        request = rquest
    }
    
    func addSubView() {
        webView = WKWebView.init(frame: self.view.bounds)
        webView?.navigationDelegate = self
        view.addSubview(webView!)
    }
    
    func addTextView(){
        
        textField = UITextField.init(frame: CGRect(x: 8, y: view.frame.size.height - 30, width: view.frame.size.width - 78, height: 30))
        textField?.backgroundColor = UIColor.purple
        textField?.placeholder = "在此输入你的网址";
        view.addSubview(textField!)
        
        let button = UIButton.init(frame: CGRect(x: view.frame.size.width - 68, y: view.frame.size.height - 30, width: 60, height: 30))
        button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        button.setTitle("go", for: UIControlState())
        button.backgroundColor = UIColor.green
        view.addSubview(button)
        
    }
    
    
    func buttonClick(_ sender:UIButton){
        
        let urlString = textField?.text

        let r = URLRequest.init(url: URL.init(string: urlString!)!)
        
        let request = r
         webView?.load(request)
     
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
         print(navigation)
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
         print(navigation)
    }
    
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
         print(navigation)
    }
    
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        print(navigation,error)
    }
    
    
    func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        
        if challenge.previousFailureCount == 0 {
            let serverTrust : SecTrust? = challenge.protectionSpace.serverTrust
            if serverTrust == nil {
                return
            }
            let cre: URLCredential = URLCredential.init(trust:serverTrust!)
//            challenge.sender?.use(cre, for: challenge) <- iOS 10.3.1 crash
            completionHandler(.useCredential,cre)
        }else{
            challenge.sender?.cancel(challenge)
            completionHandler(.cancelAuthenticationChallenge,nil)
        }

        
    }
    
   
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
